<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @package		calsses
 * @author		Terry
 * @link		
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Pagination Class
 *
 * @package		application
 * @subpackage	calsses
 * @category	Pagination
 * @author		Terry
 * @link		
 */
class Pagination {

	var $base_url			= ''; // The page we are linking to
	var $total_rows  		= ''; // Total number of items (database results)
	var $per_page	 		= 10; // Max number of items you want shown per page
	var $current_page	 		=  0; // The current page being viewed
	var $suffix				='';
	
	/**
	 * Constructor
	 *
	 * @access	public
	 * @param	array	initialization parameters
	 */
	function Pagination($params = array())
	{
		if (count($params) > 0)
		{
			$this->initialize($params);
		}
		logger('debug', "Pagination Class Initialized");
	}

	// --------------------------------------------------------------------

	/**
	 * Initialize Preferences
	 *
	 * @access	public
	 * @param	array	initialization parameters
	 * @return	void
	 */
	function initialize($params = array())
	{
		if (count($params) > 0)
		{
			foreach ($params as $key => $val)
			{
				if (isset($this->$key))
				{
					$this->$key = $val;
				}
			}
		}
	}
	
	function page_array($segment)
	{
		if($segment%$this->per_page==0)
		{
			if($segment+$this->per_page>=$this->total_rows)
			{
				$page['next_link']=$this->base_url.(string)($segment).'/'.$this->per_page.'/'.$this->suffix;
			}
			else
			{
				$page['next_link']=$this->base_url.(string)($segment+$this->per_page).'/'.$this->per_page.'/'.$this->suffix;
			}
			if($segment==0)
			{
				$page['pre_link']=$this->base_url.'0'.'/'.$this->per_page.'/'.$this->suffix;
			}
			else
			{
				$page['pre_link']=$this->base_url.(string)($segment-$this->per_page).'/'.$this->per_page.'/'.$this->suffix;
			}
			$page['first_link']=$this->base_url.'0'.'/'.$this->per_page.'/'.$this->suffix;
			$page['last_link']=$this->base_url.(floor(($this->total_rows-1)/$this->per_page)*$this->per_page).'/'.$this->per_page.'/'.$this->suffix;
			$page['total_page']=ceil($this->total_rows/$this->per_page);
			$page['current_page']=$segment/$this->per_page+1;
			$page['total_rows']=$this->total_rows;
			$page['per_page']=$this->per_page;
			$page['base_url']=$this->base_url;
			$page['segment']=$segment;
		}
		else
		{
			if($segment+$this->per_page>=$this->total_rows)
			{
				$page['next_link']=$this->base_url.(string)($segment).'/'.$this->per_page.'/'.$this->suffix;
			}
			else
			{
				$page['next_link']=$this->base_url.(string)($segment+$this->per_page).'/'.$this->per_page.'/'.$this->suffix;
			}
			if($segment==0)
			{
				$page['pre_link']=$this->base_url.'0'.'/'.$this->per_page.'/'.$this->suffix;
			}
			else
			{
				$page['pre_link']=$this->base_url.(string)($segment-$this->per_page).'/'.$this->per_page.'/'.$this->suffix;
			}
			$page['first_link']=$this->base_url.'0'.'/'.$this->per_page.'/'.$this->suffix;
			$page['last_link']=$this->base_url.(floor(($this->total_rows-1)/$this->per_page)*$this->per_page).'/'.$this->per_page.'/'.$this->suffix;
			$page['total_page']=ceil($this->total_rows/$this->per_page);
			$page['current_page']=$segment/$this->per_page+1;
			$page['total_rows']=$this->total_rows;
			$page['per_page']=$this->per_page;
			$page['base_url']=$this->base_url;
			$page['segment']=$segment;
		}
		return $page;
	}
}